package main.java.string;
/*
  char charAt（int index）
  提取字符串中指定位置的字符
 */
public class ChatAtDemo {
    public static void main(String[] args) {
        String str = "www.tedu.cn";
        //提取第五个字符
        char c = str.charAt(4);
        //输出结果
        System.out.println(c);

        //输出字符串中所有字符
        //循环遍历字符串
        for (int i = 0; i < str.length(); i++) {
            //提取当前位置的字符
            c = str.charAt(i);
            //输出字符
            System.out.println(c);
        }

        //验证回文
        String line = "上海自来水自来海上";
        /*
        //上
        //获取当前位置的字符
        char c1 = line.charAt(0);
        char c2 = line.charAt(line.length()-1-0);
        //海
        c1 = line.charAt(1);
        c2 = line.charAt(line.length()-1-1);
        //自
        c1 = line.charAt(2);
        c2 = line.charAt(line.length()-1-2);
        //来
        c1 = line.charAt(3);
        c2 = line.charAt(line.length()-1-3);*/

        //变成循环   i:0,1,2,3
        /*boolean check = true;
        for(int i=0;i<line.length()/2;i++){
            char c1 = line.charAt(i);
            char c2 = line.charAt(line.length()-1-i);
            if(c1!=c2){
                System.out.println("不是回文");
                check = false;
                break;
            }
        }
        if(check) {
            System.out.println("是回文");
        }*/


       /* for(int i=0;i<line.length()/2;i++){
            char c1 = line.charAt(i);
            char c2 = line.charAt(line.length()-1-i);
            if(c1!=c2){
                System.out.println("不是回文");
                return;
            }
        }
        System.out.println("是回文");*/


        //遍历数组
        for(int i=0;i<line.length()/2;i++){
            //提取字符
            char c1 = line.charAt(i);
            //提取倒数第i个字符
            char c2 = line.charAt(line.length()-1-i);
            //判断字符是否相等
            if(c1!=c2){
                //不相等，输出不
                System.out.print("不");
                //退出循环
                break;
            }
        }
        //如果循环正常结束，说明是回文
        System.out.println("是回文");

    }
}
